package com.rainng.coursesystem.module.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainng.coursesystem.base.api.ApiError;
import com.rainng.coursesystem.base.api.dto.BaseIdReqDTO;
import com.rainng.coursesystem.base.api.dto.PagingReqDTO;
import com.rainng.coursesystem.base.constant.Constant;
import com.rainng.coursesystem.base.exception.ServiceException;
import com.rainng.coursesystem.base.utils.BeanMapper;
import com.rainng.coursesystem.base.utils.service.RedisService;
import com.rainng.coursesystem.module.system.dto.request.SysRoleDTO;
import com.rainng.coursesystem.module.system.dto.response.SysRoleResDTO;
import com.rainng.coursesystem.module.system.entity.SysRole;
import com.rainng.coursesystem.module.system.entity.SysUser;
import com.rainng.coursesystem.module.system.mapper.SysRoleMapper;
import com.rainng.coursesystem.module.system.service.SysRoleService;
import com.rainng.coursesystem.module.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {



    @Autowired
    private SysUserService sysUserService;

    private static final String LOCK_ROLE = "sys:lock:role";
//    @Autowired
    private RedisService redisService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public IPage<SysRoleDTO> paging(PagingReqDTO<SysRoleDTO> reqDTO) {
//        IPage<SysRole> page = this.page(reqDTO.toPage());
//        //转换为dto
//        IPage<SysRoleDTO> res = JSON.parseObject(JSON.toJSONString(page), new TypeReference<Page<SysRoleDTO>>(){});
        IPage<SysRoleDTO> res = baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
        return res;

    }

    @Override
    public SysRoleDTO detail(BaseIdReqDTO reqDTO) {
        SysRole role = this.getById(reqDTO.getId());
        SysRoleDTO sysRoleDTO = new SysRoleDTO();
        BeanMapper.copy(role,sysRoleDTO);
        return sysRoleDTO;
    }

    @Override
    public void save(SysRoleDTO reqDTO) {
//        boolean lock = redisService.tryLock(LOCK_ROLE, 60000l);
//        if (!lock){
//            throw new ServiceException(ApiError.ERROR_90010008);
//        }
////        if (reqDTO.getRoleLevel() < 0 || reqDTO.getRoleLevel() > 99){
////            throw new ServiceException("新增用户角色级别必须小于99级！");
////        }
//        String userName = sysUserService.getUserName();
//        try{
////            String name = sysUserService.getUserName();
////            if (name != null && !name.equals(ApiError.ERROR_90010009)){
////                throw new ServiceException(ApiError.ERROR_90010013);
////            }
//            //编辑保存
//            if (reqDTO.getId() != null){
//                SysRole org = this.getOne(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName,reqDTO.getRoleName()).ne(SysRole::getId,reqDTO.getId()));
//                if (org != null){
//                    throw new ServiceException(ApiError.ERROR_90010011);
//                }
//                SysRole sysRole = this.getById(reqDTO.getId());
//                BeanMapper.copy(reqDTO,sysRole);
//                sysRole.setUpdateBy(userName);
//                this.updateById(sysRole);
//            }else {
//                SysRole org = this.getOne(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName,reqDTO.getRoleName()));
//                if (org != null){
//                    throw new ServiceException(ApiError.ERROR_90010011);
//                }
//                SysRole role = new SysRole();
//                BeanMapper.copy(reqDTO,role);
//                role.setCreateBy(userName);
//                //新增保存
//                this.save(role);
//            }
//        }finally {
//            redisService.del(LOCK_ROLE);
//        }
    }

    @Override
    public List<SysRoleResDTO> getList() {
        List<SysRole> list = this.list();
        List<SysRoleResDTO> collect = null;
        if (list!=null){
            collect = list.stream().map(sysRole ->
                    new SysRoleResDTO(sysRole.getId(), sysRole.getRoleName())).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public void delete(List<String> ids) {
//        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
//        wrapper.lambda().in(SysRole::getId,ids).lt(SysRole::getRoleLevel,getCurrentRoleLevel());
//        int count = this.count(wrapper);

//        if (count < ids.size()){
//            throw new ServiceException("删除角色的级别过高，禁止删除！");
//        }

        this.removeByIds(ids);
    }

    @Override
    public Integer getCurrentRoleLevel(){
//        String userName = sysUserService.getUserName();
//        JSONObject json = redisService.getJson(Constant.USER_NAME_KEY + userName);
//        SysUser user = JSONObject.toJavaObject(json, SysUser.class);
//        SysRole role = sysRoleService.getById(user.getRoleId());
//        if (role != null){
//            return role.getRoleLevel();
//        }
        return null;
    }

}
